# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/6/26 14:34
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : inform_tbl_structure.py
# @Version: planner_builder 1.0


import ctypes

from tsn_builder_struct.table_length import INFORM_TBL_LEN

c_uint32 = ctypes.c_uint32


class InformEntry_bits(ctypes.BigEndianStructure):
    _fields_ = [
        # entry1
        ('valid', c_uint32, 1),
        ('reserved1', c_uint32, 7),
        ('n_pit', c_uint32, 24),
        # entry2
        ('reserved2', c_uint32, 16),
        ('dmac16', c_uint32, 16),  # 第[47:32]位
        # entry3
        ('dmac32', c_uint32, 32),  # dmac的低32位（前32位）
        # entry4
        ('task_id', c_uint32, 16),
        ('cookies', c_uint32, 16)
    ]


class InformEntry_bytes(ctypes.BigEndianStructure):
    _fields_ = [
        ('entry1', c_uint32, 32),
        ('entry2', c_uint32, 32),
        ('entry3', c_uint32, 32),
        ('entry4', c_uint32, 32),
    ]


class InformEntry_union(ctypes.Union):
    _fields_ = [
        ('bit', InformEntry_bits),
        ('byte', InformEntry_bytes)
    ]


class InformTblEachNode:
    def __init__(self,
                 node_id: int):
        self.node_id = node_id
        self.tbl_len = INFORM_TBL_LEN
        self.tbl_each_node = []

    def append_entry(self,
                     entry: InformEntry_union):
        if len(self.tbl_each_node) <= self.tbl_len:
            self.tbl_each_node.append(entry)
        else:
            print('ERROR - inform tbl exceeds %d at node %d' % (self.tbl_len, self.node_id))
            exit(0)


class InformTbl:
    def __init__(self):
        self.tbl_set = []

    def append_tbl(self,
                   tbl: InformTblEachNode):
        self.tbl_set.append(tbl)
